/**
 * Created by HP on 2017/9/4.
 */


function setCheckedForParent(obj,states) {
    /*
     *
     * 递归去 判断是否选中 子传父法
     *
     *
     * */
    var t = obj.parent;

    obj.checked = states;


    if(t){
        setCheckedForParent(t,states);
    }
}

function setCheckedForChildren(obj,states) {
    /*
     *
     * 递归去 判断是否选中 父传子法
     *
     *
     * */
    var t = obj.children;


    if(t){
        $.each(t,function (k,v) {
            v.checked = states;
            setCheckedForChildren(v,states);
        })

    }
}


function setCheckedForBrother(obj) {
    /*
     *
     * 递归去 判断是否选中 同级选择判断 子传父法
     *
     *
     * */
    var t = obj.parent;
    var cgeck = true;
    var lrn = t.children.length;
    var now = 0;

    $.each(t.children,function (k,v) {

        if(!v.checked){
            now++;
        }

    })

    if(t && now == lrn){
        t.checked = false;
        setCheckedForBrother(t);
    }


}

function DellAllData(mainArray,statesSelectArray) {
    /*
     *
     * 循环后台返回的json数据
     *
     * create by millent 2017年9月4日17:22:49
     *
     * */

    function isChecked(obj) {
        $.each(statesSelectArray,function (k,v) {

            // console.log(v)
            for(var i=0;i<v.length;i++){
                if(v[i].id == obj.id){
                    obj.checked = true;
                }
            }




        })
    }

    var nowJson01 = [];// 循环第一集
    var nowJson02 = [];// 循环第二集
    var nowJson03 = [];// 循环第三集
    var nowJson04 = [];// 循环第四集

    $.each(mainArray,function (k,v) {

        var g = v;
        g.children = [];
        g.parent = null;
        g.checked = false;
        g.active = true

        if(v.MenuGroup == 0){
            nowJson01.push(g);
        }
        if(v.MenuGroup == 1){
            nowJson02.push(g);
        }
        if(v.MenuGroup == 2){
            g.active = false
            nowJson03.push(g);
        }
        if(v.MenuGroup == 3){
            g.active = false
            nowJson04.push(g);
        }
    })

    // 组装已经循环好的数据
    // 反向拼接法 从后到前拼接

    // 四级转三级别
    $.each(nowJson03,function (k1,v1) {
        $.each(nowJson04,function (k2,v2) {
            if(v1.id == v2.MenuFid){
                v2.children = null;
                v2.parent = v1;

                v1.children = v1.children || [];
                v1.children.push(v2)
            }
            isChecked(v2);
        })
    })

    // 四级转三级别
    $.each(nowJson02,function (k1,v1) {
        $.each(nowJson03,function (k2,v2) {
            if(v1.id == v2.MenuFid){
                v2.parent = v1;

                v1.children = v1.children || [];
                v1.children.push(v2)
            }
            isChecked(v2);
        })
    })

    // 三级转二级别
    $.each(nowJson01,function (k1,v1) {
        $.each(nowJson02,function (k2,v2) {
            if(v1.id == v2.MenuFid){
                v2.parent = v1;

                v1.children = v1.children || [];
                v1.children.push(v2)
            }
            isChecked(v2);
        })
    })

    // 一级整理
    $.each(nowJson01,function (k1,v1) {
        isChecked(v1);
    })



    //初始化选中状态
    $.each(nowJson04,function (k1,v1) {
        if(v1.checked){
            setCheckedForParent(v1,true);
        }
    })
    console.log(nowJson04)
    return nowJson01;
}


var Vm = new Vue({
    el: "#app",
    data: {
        menu: [],
        disabled : false,
        google:[],
        apple:"",
        username: "未登录",

        quanxianShow: false,
        nasaPlane:{
            mark:1,//	string	是	标记 1 添加 2 修改 3 删除	3
            groupName:'',//	string	是	权限组名	宣传部
            groupStatus:1,//	string	是	权限组状态	1
            createUser:rose.util.getItem('admin_empname'), //	string	是	创建人	Admin
            groupClass:1 //	string	是	权限UUid( 2 修改 3 删除 必须 )	4e9efceb-a96b-4f8d-a946-b4a14d61983a
        },


        nowBedApple: false,


        // 是否显示添加权限组
        isShowAddGroup: false
    },
    methods : {
        microsoftChange : function (object) {
            console.log(object,'this IDS')
            try{
                if(object.checked){
                    setCheckedForParent(object,true);
                    //console.log(object.parent)

                }else{
                    setCheckedForBrother(object)
                }
            }catch (e){}

            setCheckedForChildren(object,object.checked)



        },
        microsoftUpLoad : function () {

            var backJava = [];

            var root = this;
            // 这一块是处理参数传递的          例如  ID
            $.each(this.menu,function (k1,v1){
                $.each(v1.children,function (k2,v2) {

                    var betaD = {
                        id : v2.id,
                        value : ''

                    }
                    // 累死了，这逻辑。。。
                    $.each(v2.children,function (k3,v3) {

                        if(v3.MenuDoUrl && v3.checked){
                            betaD.value+= '1'
                        }else if(v3.MenuDoUrl){
                            betaD.value+= '0'
                        }

                        var betaA = {
                            id : v3.id,
                            value : ''
                        }
                        $.each(v3.children,function (k4,v4) {

                            if(v4.MenuDoUrl && v4.checked){
                                betaA.value+= '1'
                            }else if(v4.MenuDoUrl){
                                betaA.value+= '0'
                            }

                        })
                        if(betaA.value && parseInt(betaA.value) > 0) {
                            backJava.push(betaA);
                        }

                    })
                    if(betaD.value && parseInt(betaD.value) > 0){
                        backJava.push(betaD);
                    }

                })
            })
            // findChildren(backJava,this.menu);

            var root = this;
            var arrs=getCookie('yanzheng');             //徐修改
            console.log(arrs)
            arrs=arrs.split('|');
            var ids=arrs[1];

            if(!this.disabled){
                this.disabled = true;
                console.log(JSON.stringify({
                    compid : ids,
                    groupClass:root.apple,
                    list : backJava
                }))




                var close = root.console.load();
                API.adminSavePermiss(JSON.stringify({
                    compid : localStorage.getItem('admin_compid'),
                    groupClass:root.apple,
                    list : backJava
                }),function (data) {
                    close();
                    root.disabled = false;
                    STATES.isSuccess(data,function (filterData) {
                        root.console.log(STATES.outCode(filterData.msg))
                    },function (filterData) {
                        root.console.log(STATES.outCode(filterData.msg))
                    })
                },function () {
                    close();
                    root.disabled = false;
                })
            }

            console.log(backJava)

        },
        toggleShow : function (obj) {
            obj.active = !obj.active;
        },
        chouseGoogle: function (x) {

            this.menu = [];
            this.disabled = false;
            //加载权限组
            var load = this.console.load();
            var root = this;
            API.loadPermi({
                mark: 1
            },function (data) {
                // if(data.)
                if(data.data.length<=0){
                    return;
                }

                STATES.isSuccess(data,function (d) {
                    root.google = d.data;

                    API.adminLoadMenu({         //加载权限菜单

                    },function (data) {
                        console.log(data,'>>>>>>>>>>>>')
                        var taskData = data.data[0];
                        root.nowBedApple = root.apple || 1;
                        API.adminLoadPermiSelected({
                            id : root.apple || 1
                        },function (dataBase) {             //加载选中的
                            root.menu = DellAllData(taskData,dataBase.data);
                            console.log(root.menu)
                            load();
                        })

                    },function () {
                        load()
                        root.console.log("加载失败");
                    })
                },function (d) {

                })
            })
        },
        linkGo:function (link) {
            this.openPage(link);
            rose.util.removeItem('admin_compid');
            rose.util.removeItem('admin_token');
            rose.util.removeItem('admin_empname');
            rose.util.removeItem('admin_compid');
            rose.util.removeItem('admin_api');
        },
        nasaAdd: function () {
            // 添加权限组_显示
            var root = this;

            if($.trim(this.nasaPlane.groupName) == ''){
                this.console.log('权限组名不能为空');
            }else{
                var load = this.console.load();

                /*
                API.editPermiGroup(this.nasaPlane,function (e) {

                    STATES.isSuccess(e,function (data) {
                        root.console.log("权限组添加成功");
                        root.nasaPlane.groupName = ""
                        API.loadPermi({
                            mark: 1
                        },function (data) {
                            STATES.isSuccess(data,function (d) {
                                root.google = d.data[0];

                            },function (d) {

                            })
                        })

                    },function (data) {
                        root.console.log(data.msg);
                    })
                    load()

                },function () {
                    load()
                })
                */
                var backJava = [];
                var root = this;
                $.each(this.menu,function (k1,v1) {
                    $.each(v1.children,function (k2,v2) {

                        var betaD = {
                            id : v2.id,
                            value : ''
                        }
                        // 累死了，这逻辑。。。
                        $.each(v2.children,function (k3,v3) {

                            if(v3.MenuDoUrl && v3.checked){
                                betaD.value+= '1'
                            }else if(v3.MenuDoUrl){
                                betaD.value+= '0'
                            }

                            var betaA = {
                                id : v3.id,
                                value : ''
                            }
                            $.each(v3.children,function (k4,v4) {

                                if(v4.MenuDoUrl && v4.checked){
                                    betaA.value+= '1'
                                }else if(v4.MenuDoUrl){
                                    betaA.value+= '0'
                                }

                            })
                            if(betaA.value && parseInt(betaA.value) > 0) {
                                backJava.push(betaA);
                            }

                        })
                        if(betaD.value && parseInt(betaD.value) > 0){
                            backJava.push(betaD);
                        }

                    })
                })


                // console.log(JSON.stringify({
                //     groupName : this.nasaPlane.groupName,
                //     createUser:rose.util.getItem('admin_empname'),
                //     mark: 0,
                //     list : backJava
                // }))

                var arrs=getCookie('yanzheng');
                arrs=arrs.split('|');

                API.adminSavePermiss(JSON.stringify({
                    groupName : this.nasaPlane.groupName,
                    createUser:arrs[0],
                    mark: 0,
                    compid:arrs[1],
                    p:getCookie('yanzheng'),
                    list : backJava
                }),function (data) {

                    STATES.isSuccess(data,function (filterData) {
                        load()
                        root.console.log(STATES.outCode(filterData.msg))

                        setTimeout(function () {
                            window.location.reload();
                        },1000)
                    },function (filterData) {
                        load()
                        root.console.log(STATES.outCode(filterData.msg))

                    })


                })

            }
        },
        deleteBedApple: function () {
            var root = this;
            var load = this.console.load();

            if(this.google.length == 1){
                alert('必须保留一个权限组');
                load();
                return;
            }

            API.editPermiGroup({
                mark: 3,
                p:getCookie('yanzheng'),
                type:'c',
                groupClass: this.nowBedApple,
                createUser: rose.util.getItem('admin_empname')
            },function (data) {
                try{
                    var g = data.msg;
                    console.log(g)
                    root.console.log(g)
                    if(g.indexOf('成功') != -1){
                        setTimeout(function () {
                            location.reload()
                        },1000)
                    }
                }catch (e){
                }
                load();
            },function () {
                load();
                root.console.log(STATES.netError());
            })
            console.log(this.nowBedApple);
        },


        // 权限组添加之前的准备
        beforeAddGroup: function () {
            this.isShowAddGroup = !this.isShowAddGroup;

            function removeChecked(data) {
                for(var i=0; i<data.length; i++){
                    data[i].checked = false;
                    if(data[i].children){
                        removeChecked(data[i].children)
                    }
                }
            }
            removeChecked(this.menu);

            console.log(this.menu,'<----')
            // 把form组的选中都去掉

        }
    },
    computed : {
        isAnFunction : function () {
            return function (data) {
                return data.MenuDoUrl == "" ? true : false;
            }
        }
    },
    mounted : function () {
        var root = this;

        root.username = rose.util.getItem('admin_empname');

        //加载权限组
        var load = this.console.load();
        API.loadPermi({
            mark: 1
        },function (data) {

            STATES.isSuccess(data,function (d) {
                root.google = d.data;

                API.adminLoadMenu({

                },function (data) {
                    var taskData = data.data[0];
                    console.log(data.data, 'test 0000!!!!!!' )
                    try{
                        /*徐         2017年12月25日11:39:19  修改*/
                        if(d.data.length<=0){
                            return;
                        }

                        root.apple = d.data[0].id               //默认选中
                    }catch (e){

                        root.console.log("获取数据失败")
                        load();
                        return;
                    }
                    root.nowBedApple = d.data[0].id || 1;
                    console.log(root.nowBedApple)
                    API.adminLoadPermiSelected({
                        id :  d.data[0].id || 1
                    },function (dataBase) {
                        console.log(dataBase, '++++++++++++++++=====')
                        console.log(taskData,'------------------')
                        root.menu = DellAllData(taskData,dataBase.data);
                        // console.log(root.menu)
                        load();
                    })

                },function () {
                    load()
                    root.console.log("加载失败");
                })
            },function (d) {
                
            })
        })
    }
})
